
const Service = require('egg').Service;
const sha1 = require("sha1")


class Back extends Service {


    async getQuestionByType(type, start) {

        var sum = start * 15
        var nextStart = -1
       
       if (type == "all") {
            var questionList = await this.app.mysql.select("questions", {
                where: {
                },
               orders:[["postime","desc"]],
                limit: sum
            })
        } else {
            var questionList = await this.app.mysql.select("questions", {
                where: {
                    type
                },
                limit: sum,
                orders:[["postime","desc"]]
            })
        }
        if (questionList) {
            if (questionList.length > 0) {
                for (var i = 0; i < questionList.length; i++) {
                    var q_id = questionList[i].id
                    var options = await this.app.mysql.select("question_detail", {
                        where: {
                            q_id
                        }
                        
                    })
                    questionList[i].options = options
                }
            }

            if (questionList.length >= sum) {
                nextStart = start + 1
            }
            return {
                status: 1,
                questionList,
                message:"ok"
            }
        } else {
            return {
                status: 0
            }
        }
    }



  async addQuestion(form, dynamicTags) {
         console.log(form.more)
        if (form.more&&form.more!="false") {
            var newQuestion = await this.app.mysql.insert("questions", {
                title: form.title,
                description: form.description,
                type: form.type,
                more: 1
            })
            var q_id = newQuestion.insertId
            for (var i = 0; i < dynamicTags.length; i++) {
                var option = dynamicTags[i]
                var newOption = await this.app.mysql.insert("question_detail", {
                    title: option.inputValue,
                    status: option.checked=="true"?1:0,
                    q_id,
                })
            }

        }else{
              var newQuestion = await this.app.mysql.insert("questions", {
                title: form.title,
                description: form.description,
                type: form.type,
                more: 0
            })
            var q_id = newQuestion.insertId
            for (var i = 0; i < dynamicTags.length; i++) {
                var option = dynamicTags[i]
                var newOption = await this.app.mysql.insert("question_detail", {
                    title: option.inputValue,
                    status: option.status==1?1:0,
                    q_id,
                })
            }
        }

        if(newQuestion.affectedRows==1){
            return{
                status:1,
                message:"ok"
            }
        }else{
            return {
                status:0
            }
        }

    }


    async deleteQuestion(id) {

        var delTitle = await this.app.mysql.delete("questions", {
            id
        })

        var delOption = await this.app.mysql.delete("question_detail", {
            q_id: id
        })

        if (delTitle.affectedRows == 1) {
            return {
                status: 1,
                message: "ok"
            }
        } else {
            return {
                status: 0
            }
        }
    }


}

module.exports = Back;
